package com.heima.behavior.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //参数校验
        if(dto==null||dto.getArticleId()==null||(dto.getOperation()<0||dto.getOperation()>1)||
                (dto.getType()<0||dto.getType()>2)
        ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if(apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询点赞行为是否存在
        ApLikesBehavior apLikesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
        );
        //不存在 是否有取消点赞行为
        if (apLikesBehavior==null&&dto.getOperation().equals(ApLikesBehavior.Operation.CANCEL.getCode())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"暂无点赞");
        }
        //存在 判断是否重复点赞
        if (apLikesBehavior!=null&&dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())
                &&apLikesBehavior.getOperation()==0&&dto.getOperation().intValue()==apLikesBehavior.getOperation().intValue()
        ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"不可重复点赞");
        }

        //保存点赞
        //点赞行为不存在,且前端传来的参数为点赞 0,就保存
        if (apLikesBehavior==null&&dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())){
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setType(dto.getType());
            save(apLikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            //取消点赞
            apLikesBehavior.setOperation(dto.getOperation());
            updateById(apLikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }



    }
}
